import { ref } from "vue";
import type { OptionsType, ListType } from '@/axios/api/type.ts'
import { getAllHospList, getWarnLevel, getValidExcludeList } from '@/axios/api/options_api'
import { validModifyState, validExcludeSave } from '@/axios/api/valid_api'
import { find } from 'lodash-es'
import { Modal, message } from 'ant-design-vue';
const [messageApi] = message.useMessage();
interface ExcludeType {
  hospId: string,
  validId: string,
  validRemark: boolean,
  excludeId?: string
}

export const useValidHook = () => {
  const hospList = ref<OptionsType[]>([])
  const excludeList = ref<ExcludeType[]>([])
  const warnLevelList = ref<ListType[]>([])
  const formInfo = ref({
    hospId: '',
    hospName: ''
  })
  const param = ref({})

  function selectChange(value: string) {
    formInfo.value.hospId = value;
    formInfo.value.hospName = hospList.value.find(item => item.id === value)?.desc || '';
    getValidExcludeOption();
  }
  async function getOptionList() {
    const hospInfo = await getAllHospList().catch(() => {
      return {
        code: 400,
        data: []
      }
    })
    hospList.value = hospInfo.data
    const warnInfo = await getWarnLevel().catch(() => {
      return {
        code: 400,
        data: []
      }
    })
    warnLevelList.value = warnInfo.data
  }
  async function getValidExcludeOption() {
    const excludeInfo = await getValidExcludeList({ hospId: formInfo.value.hospId }).catch(() => {
      return {
        code: 400,
        data: []
      }
    })
    excludeList.value = excludeInfo.data
  }
  function handleForbiddened(record: any) {
    Modal.confirm({
      title: '修改校验规则',
      content: `${formInfo.value.hospName}医院${record.isForbiddened ? ' 启用 ' : ' 排除 '}此校验规则?`,
      centered: true,
      class: 'common_modal',
      onOk() {
        if (record.isForbiddened) {
          validModifyState({ id: record.excludeId }).then(res => {
            if (res.code === 200) {
              messageApi.info(res.msg);
            }
          })
        } else {
          validExcludeSave({
            hospId: formInfo.value.hospId,
            hospName: formInfo.value.hospName,
            validInfos: [
              {
                validId: record.id,
                validRemark: record.remark
              }
            ]
          }).then(res => {
            if (res.code === 200) {
              messageApi.success(res.msg);
            }
          })
        }
      },
      onCancel() {
        console.log('%c [ handleForbiddened ]-148', 'font-size:13px; background:pink; color:#bf2c9f;');
      }
    });
  }
  function getCustomCol({ val, callback }: { val: any; callback: any }) {
    const list = val.map((item: any) => {
      const info = find(excludeList.value, (f: any) => f.validId === item.id)
      if (info) {
        item.isForbiddened = true
        item.excludeId = info.excludeId
        item.hospId = info.hospId
      } else {
        item.isForbiddened = false
        item.excludeId = ''
        item.hospId = ''
      }
      return item
    })
    callback(list)
  }

  return {
    param,
    hospList,
    formInfo,
    excludeList,
    warnLevelList,
    getOptionList,
    handleForbiddened,
    selectChange,
    getValidExcludeOption,
    getCustomCol
  }
};

